package com.jymj.service.impl;

import com.jymj.dao.LandDao;
import com.jymj.entity.SysUserEntity;
import com.jymj.entity.SysXzqFhEntity;
import com.jymj.entity.TdlyEntity;
import com.jymj.entity.data.KxTxHzEntity;
import com.jymj.entity.data.XzDataEntity;
import com.jymj.service.LandService;
import com.jymj.utils.ShiroUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ljj on 2017/3/17.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class LandServiceImpl implements LandService {

    @Resource
    private LandDao landDao;

    @Override
    public TdlyEntity getTdlyByPoint(String point, String code) {
        Map map = new HashMap();
        map.put("point", point);
        map.put("code", code);
        return landDao.getTdlyByPoint(map);
    }

    @Override
    public List<TdlyEntity> getTdlyQueryFrame(Map map) {
        return landDao.getTdlyQueryFrame(map);
    }

    @Override
    public List<TdlyEntity> getTdlyListExcel(Map map1) {
        return landDao.getTdlyListExcel(map1);
    }

    @Override
    public Integer getTdlyListCount(Map map) {
        return landDao.getTdlyListCount(map);
    }

    @Override
    public XzDataEntity getXzData(String points, String code) {
        String transformStr = landDao.getTransformStr(points);
        StringBuilder builder = new StringBuilder("SELECT * FROM (select sum(tt.rk) as ztrk,sum(tt.hucount) as hs,");
        builder.append("sum(tt.nongcount) as ncount,sum(tt.feinongcount) as feincount from yl tt where ST_Intersects('");
        builder.append(transformStr);
        builder.append("',tt.geom)=true) a1,");
        builder.append("(select sum(case when yl_type=1 then area ELSE 0 end)/10000 as zhaimj,");
        builder.append("sum(case when yl_type=1 then jianzhu_area ELSE 0 end)/10000 as zhaijzmj,");
        builder.append("sum(case when yl_type=1 then 1 ELSE 0 end) as zhaicount,");
        builder.append("sum(case when yl_type in (2,3) then area ELSE 0 end)/10000 as feicount,");
        builder.append("sum(case when yl_type in (2,3) then area ELSE 0 end)/10000 as feimj,");
        builder.append("sum(case when yl_type in (2,3) then jianzhu_area ELSE 0 end)/10000 as feijzmj ");
        builder.append("from yl y where ST_Intersects('");
        builder.append(transformStr);
        builder.append("',y.geom)=true and y.code like CONCAT('%','");
        builder.append(code);
        builder.append("','%')) a2,(select sum(st_area(ST_Intersection('");
        builder.append(transformStr);
        builder.append("',t.geom)))/10000 as jsydmj from tdly t where t.type=3 and t.qsxz not in ('10','20')) a3,");
        builder.append("(select sum(st_area(ST_Intersection('");
        builder.append(transformStr);
        builder.append("',t.geom)))/10000 as nydmj from tdly t where t.type=1 and t.qsxz not in ('10','20')) a4,(select sum(st_area(ST_Intersection('");
        builder.append(transformStr);
        builder.append("',t.geom)))/10000 as gdmj from tdly t where t.dlbm in('012','013','011') and t.type=1 and t.qsxz not in ('10','20')) a5,");
        builder.append("(select sum(st_area(ST_Intersection('");
        builder.append(transformStr);
        builder.append("',t.geom)))/10000 as wlydmj from tdly t where t.type=2 and t.qsxz not in ('10','20')) a6");
        builder.append(",(select sum(st_area(ST_Intersection('");
        builder.append(transformStr);
        builder.append("',t.geom)))/10000 as gyjsydmj from tdly t where t.type=3 and t.qsxz in ('10','20')) a7,");
        builder.append("(select sum(st_area(ST_Intersection('");
        builder.append(transformStr);
        builder.append("',t.geom)))/10000 as gynydmj from tdly t where t.type=1 and t.qsxz not in ('10','20')) a8,(select sum(st_area(ST_Intersection('");
        builder.append(transformStr);
        builder.append("',t.geom)))/10000 as gygdmj from tdly t where t.dlbm in('012','013','011') and t.type=1 and t.qsxz not in ('10','20')) a9,");
        builder.append("(select sum(st_area(ST_Intersection('");
        builder.append(transformStr);
        builder.append("',t.geom)))/10000 as gywlydmj from tdly t where t.type=2 and t.qsxz not in ('10','20')) a10");
        return landDao.getXzData(builder.toString());
    }

    @Override
    public List<KxTxHzEntity> getTxKxHz(String points, String code) {
        Map map = new HashMap();
        map.put("points", points);
        map.put("code", code);
        return landDao.getTxKxHz(map);
    }

    @Override
    public XzDataEntity getXzDataBycode(String[] codeList) {
        Map map = new HashMap();
        map.put("codeList", codeList);
        return landDao.getXzDataBycode(map);
    }

    @Override
    public List<TdlyEntity> queryTdly(List codeList, String code) {
        Map map = new HashMap();
        map.put("codeList", codeList);
        map.put("code", code);
        return landDao.queryTdly(map);
    }

    @Override
    public List<TdlyEntity> getTdlyList(Integer objectid) {
        return landDao.getTdlyList(objectid);
    }

    @Override
    public List<TdlyEntity> getFeiKxTxHz(String points, String code) {
        Map<String, Object> map = new HashMap<>();
        map.put("points", points);
        map.put("code", code);
        return landDao.getFeiKxTxHz(map);
    }

    @Override
    public BigDecimal getArea(String points, String code, Integer type) {
        Map<String, Object> map = new HashMap<>();
        map.put("points", points);
        map.put("code", code);
        map.put("type",type);
        return landDao.getArea(map);
    }

    /**
     * 查询point是否在权限范围内
     * @param point
     * @return
     */
    @Override
    public boolean queryIstrue(String point) {
        Map map = new HashMap();
        map.put("point",point);
        SysUserEntity user = ShiroUtils.getUserEntity();
        if (user!=null && user.getXzqs()!=null && user.getType() != null && user.getType()!= 1 && user.getType()!= 2 && user.getType()!= 4) {
            List<SysXzqFhEntity> xzqList = user.getXzqs();
            map.put("xzqs",xzqList);
        } else if(user!=null && user.getCode()!=null){
            String code  = user.getCode();
            map.put("code",code);
        }
        int count = landDao.queryIstrue(map);
        if(count>=1){
            return true;
        }
        return false;
    }
}
